/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.constants.ControlCharacter;
import com.xy.dto.ExhibitionDTO;
import com.xy.entity.*;
import com.xy.excel.ExhibitionExcel;
import com.xy.mapper.ExhibitionMapper;
import com.xy.mapper.ViewerMapper;
import com.xy.service.*;
import com.xy.tx.TxCommon;
import com.xy.vo.ExhibitionVO;
import com.xy.wrapper.ExhibitionWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 展会表 服务实现类
 *
 * @author BladeX
 * @since 2023-07-18
 */
@Service
@Slf4j
@AllArgsConstructor
public class ExhibitionServiceImpl extends BaseServiceImpl<ExhibitionMapper, ExhibitionEntity> implements IExhibitionService {

	private final ViewerMapper viewerMapper;
	private final IExhibitionPositionService exhibitionPositionService;
	private final IExhibitionPressingService exhibitionPressingService;
	private final IExhibitionConfigService exhibitionConfigService;

	@Override
	public IPage<ExhibitionVO> selectExhibitionPage(IPage<ExhibitionVO> page, ExhibitionVO exhibition) {
		return page.setRecords(baseMapper.selectExhibitionPage(page, exhibition));
	}

	@Override
	public Long selectExhibitionNum(ExhibitionDTO exhibitionEntity) {
		return baseMapper.selectCount(Wrappers.<ExhibitionEntity>query().lambda().eq(exhibitionEntity.getVenueId()!=null,ExhibitionEntity::getVenueId,exhibitionEntity.getVenueId())
			.eq(exhibitionEntity.getProjectId()!=null,ExhibitionEntity::getProjectId,exhibitionEntity.getProjectId()));
	}


	@Override
	public List<ExhibitionExcel> exportExhibition(Wrapper<ExhibitionEntity> queryWrapper) {
		List<ExhibitionExcel> exhibitionList = baseMapper.exportExhibition(queryWrapper);
		//exhibitionList.forEach(exhibition -> {
		//	exhibition.setTypeName(DictCache.getValue(DictEnum.YES_NO, Exhibition.getType()));
		//});
		return exhibitionList;
	}

	@Override
	public List<ExhibitionEntity> queryExhibition(){
		LambdaQueryWrapper<ExhibitionEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ExhibitionEntity::getCloseStatus, ControlCharacter.QUERY_STAUTS);
		List<ExhibitionEntity> exhibitionEntityList = this.list(wrapper);
		return  exhibitionEntityList;
	}

	@Override
	public ExhibitionVO getExhibitionDetail(ExhibitionEntity exhibition) {
		ExhibitionEntity detail = this.getOne(Condition.getQueryWrapper(exhibition));
		ExhibitionVO exhibitionVO = ExhibitionWrapper.build().entityVO(detail);
		data(exhibitionVO);
		return exhibitionVO;
	}

	@Override
	public IPage<ExhibitionVO> pageExhibitionVO(Map<String, Object> exhibition, Query query) {
		QueryWrapper<ExhibitionEntity> queryWrapper = Condition.getQueryWrapper(exhibition,ExhibitionEntity.class);
		queryWrapper.lambda().orderByDesc(ExhibitionEntity::getCreateTime);
		IPage<ExhibitionEntity> pages = this.page(Condition.getPage(query),queryWrapper);
		IPage<ExhibitionVO> pageVO = ExhibitionWrapper.build().pageVO(pages);
		List<ExhibitionVO> voList = pageVO.getRecords();
		voList.stream().forEach(o->{
			data(o);
		});
		return pageVO.setRecords(voList);
	}

	@Override
	public Boolean saveOrUpdateExhibition(ExhibitionEntity exhibition) {
		Long id = exhibition.getId();
		this.saveOrUpdate(exhibition);
		log.info("创建展会成功,exhibition={}",exhibition);
		if(id == null ){
//			// 初始化一个AipFace
//			AipFace client = AipFaceClient.getUniqueInstance();
//			// 创建用户组
//			JSONObject res = client.groupAdd(exhibition.getId().toString(), null);
//			log.info("百度用户组创建返回结果：{}",res);
			//创建腾讯用户组
			TxCommon.createGroup(exhibition.getExhibitionName(),String.valueOf(exhibition.getId()));
			ExhibitionConfigEntity exhibitionConfig = BeanUtil.newInstance(ExhibitionConfigEntity.class);
			exhibitionConfig.setExhibitionId(exhibition.getId());
			exhibitionConfigService.save(exhibitionConfig);
			log.info("创建展会配置成功,exhibitionConfig={}",exhibitionConfig);
		}

		return true;
	}

	/**
	 * 组装数据
	 */
	public void data(ExhibitionVO o){
		LambdaQueryWrapper<ViewerEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ViewerEntity::getExhibitionId,o.getId());
		long viewerCount = viewerMapper.selectCount(wrapper);
		o.setViewerNum(viewerCount);
		LambdaQueryWrapper<ExhibitionPositionEntity> positionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
		positionEntityLambdaQueryWrapper.eq(ExhibitionPositionEntity::getExhibitionId,o.getId());
		long positionCount = exhibitionPositionService.count(positionEntityLambdaQueryWrapper);
		o.setPositionNum(positionCount);
		LambdaQueryWrapper<ExhibitionPressingEntity> pressingEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
		pressingEntityLambdaQueryWrapper.eq(ExhibitionPressingEntity::getExhibitionId,o.getId());
		long pressingCount = exhibitionPressingService.count(pressingEntityLambdaQueryWrapper);
		o.setPressingNum(pressingCount);
	}

}
